<template>
  <div class="mod-discount">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :option="tableOption"
      @search-change="searchChange"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="getDataList"
    >
      <template slot="menuLeft">
        <el-button type="primary" icon="el-icon-plus" size="small" v-if="isAuth('admin:discount:save')" @click="addOrUpdateHandle()">{{$t("crud.addTitle")}}</el-button>
      </template>

      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small">{{$t("station.close")}}</el-tag>
        <el-tag v-else-if="scope.row.status === 1" size="small" type="success">{{$t("groups.turnOn")}}</el-tag>
        <el-tag v-else-if="scope.row.status === 2" size="danger">{{$t("product.violation")}}</el-tag>
        <el-tag v-else-if="scope.row.status === 3" size="warning">{{$t("product.pendingReview")}}</el-tag>
      </template>

      <template slot-scope="scope" slot="menu">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          v-if="isAuth('admin:discount:update')"
          @click="addOrUpdateHandle(scope.row.discountId)"
        >{{$t("groups.editEvent")}}</el-button>
        <el-button
          type="primary"
          icon="el-icon-view"
          size="mini"
          v-if="isAuth('admin:discount:discountProd')"
          @click="showProds(scope.row.discountId)"
        >{{$t("live.viewProduct")}}</el-button>
        <el-button
          type="primary"
          icon="el-icon-document-checked"
          size="mini"
          v-if="isAuth('admin:discount:auditApply') && scope.row.status > 1"
          @click="offlineEventHandle(scope.row.discountId)"
        >{{scope.row.status === 2?$t("groups.applyForListing"):$t("coupon.waitReview")}}</el-button>
        <el-button
          style="margin-top:10px"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          v-if="isAuth('admin:discount:delete')"
          @click="deleteHandle(scope.row.discountId)"
        >{{$t("text.delBtn")}}</el-button>
      </template>
    </avue-crud>
    <!-- 弹窗  新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate"></add-or-update>

    <!-- 下线管理弹窗-->
    <offlineEventHandle
      v-if="offlineEventHandleVisible"
      selectUrl="/admin/discount/getOfflineHandleEventByDiscountId"
      applyUrl="/admin/discount/auditApply"
      ref="offlineEventHandle"
      @refreshDataList="refreshChange"
    ></offlineEventHandle>
  </div>
</template>

<script>
import AddOrUpdate from './discountProd-add-or-update'
import { tableOption } from '@/crud/marketing/discount'
import OfflineEventHandle from '@/components/offline-event-handle'

export default {
  data () {
    return {
      dataForm: {
        discountName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      offlineEventHandleVisible: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      tableOption: tableOption

    }
  },
  components: {
    AddOrUpdate,
    OfflineEventHandle
  },
  methods: {
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/admin/discount/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增
    addOrUpdateHandle (id) {
      this.$router.push({ path: '/discountInfo', query: { discountId: id } })
    },
    // 查看商品
    showProds (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.showProdInit(id, 1)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`${this.$i18n.t('sys.makeSure')}[${this.$i18n.t('text.delBtn')}]${this.$i18n.t('text.menu')}?`, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/admin/discount/' + id),
            method: 'delete',
            data: this.$http.adornData({}, false)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        })
        .catch(() => { })
    },
    // 条件查询
    searchChange (params) {
      this.searchForm = params
      this.page.currentPage = 1
      this.page.pageSize = 10
      this.getDataList(this.page)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    // 下线管理
    offlineEventHandle (id) {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEventHandle.init(id)
      })
    },
    /**
     * 刷新数据
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList()
    }
  }
}
</script>
